package com.example.RuanJianKeShe.Dao.impl;

import com.example.RuanJianKeShe.Bean.Student;
import com.example.RuanJianKeShe.Dao.StudentDao;
import com.example.RuanJianKeShe.Utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 获取所有学生信息
     *
     * @return List<Student>
     */
    @Override
    public List<Student> getAllStudent() {
        String sql = "select * from Student";
        try {
            List<Student> studentList = template.query(sql,new BeanPropertyRowMapper<Student>(Student.class));
            return studentList;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 通过学号和密码获取学生信息
     *
     * @param id  String
     * @param key String
     * @return Student
     */
    @Override
    public Student getStudentByIdAndKey(int id, String key) {
        String sql = "select * from Student where sno = ? and stuKey = ?";
        try {
            Student student = template.queryForObject(sql,new BeanPropertyRowMapper<Student>(Student.class),id,key);
            return student;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 通过学号查找学生
     *
     * @param id int
     * @return Student
     */
    @Override
    public Student getStudentById(int id) {
        String sql = "select * from Student where sno = ?";
        try {
            Student student = template.queryForObject(sql,new BeanPropertyRowMapper<Student>(Student.class),id);
            return student;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过学号修改学生信息
     *
     * @return Student
     */
    @Override
    public Boolean setStudentById(Student stu) {
        String sql = "update Student set sname=?,sage=?,ssex=?,telephone=?,email=?,stukey=?,createTime=?  where sno=? ";
        try {
             template.update(sql,stu.getSname(),stu.getSage(),stu.getSsex(),
                     stu.getTelephone(),stu.getEmail(),stu.getStukey(),stu.getCreateTime(),stu.getSno());
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 通过学生类来添加学生信息
     *
     * @param stu Student
     * @return boolean
     */
    @Override
    public boolean addStudent(Student stu) {
        String sql = "insert into Student(sname,sage,ssex,telephone,email,stukey,createTime) values(?,?,?,?,?,?,?)";
        try{
            template.update(sql,stu.getSname(),stu.getSage(),stu.getSsex(),
                    stu.getTelephone(),stu.getEmail(),stu.getStukey(),stu.getCreateTime());
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 通过学号删除学生
     *
     * @param id int
     * @return boolean
     */
    @Override
    public boolean delStudent(int id) {
        String sql = "delete from student where sno = ?";
        try{
            template.update(sql,id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }


        return false;
    }

    /**
     * 登录
     *
     * @param id  学号
     * @param key 密码
     * @return 学生对象
     */
    @Override
    public Student login(int id, String key) {
        String sql= "Select * from student where sno = ? and stukey = ?";
        try{
            Student student = template.queryForObject(sql,new BeanPropertyRowMapper<Student>(Student.class),id,key);
            return student;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
